use Eurobarom_2015_2009_imputed.dta, clear

label var unempl "Unemployment rate"
label var chng_gdp "Growth change"
*replace une_chng_crisis = chng_une if EB!=2015 & une_chng_crisis==.

*Slovenia and Poland emerge as outliers. See analyses_append do-file. 

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis i.EB ///
	|| v6: , mle variance
eststo M1

/* 
*Figure 1: Effect of Unemployment on Knowledge
margins, at(une_chng_crisis=(-5(1)20)) predict (mu fixed)
marginsplot, recast(connected) recastci(rline) ///
	xtitle("{&Delta}Unemployment since 2007") ytitle("Probability of Correct Response") ///
	ylabel(0(.25)1) xlabel(-5(5)20) scheme(s2mono)

*Predicted probability for unemployed vs. working	
margins, at(mean_une_knowl_lag2007=.30 female=1 age=49.79 age_2=2771.23 ///
	educ_yrs=19.83 not_laborforce=0 student=0 hh_children=1 EB=2015 ///
	unemployed=(0 1) une_chng_crisis=0) predict (mu fixed)
*/

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.EB ///
	|| v6: unemployed , mle variance cov(unstructured)
eststo M2
/* 
*Figure 2: Interaction between Unemployed and Unemployment
margins, at(une_chng_crisis=(-5(1)20) unemployed=(0(1)1)) predict (mu fixed)
 marginsplot, recast(connected) recastci(rline) ///
	xtitle("{&Delta}Unemployment since 2007") ytitle("Probability of Correct Response") ///
	ylabel(0(.25)1) xlabel(-5(5)20) 
	graph display, scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))
	
margins, dydx(unemployed) at(une_chng_crisis=(-5(1)20)) predict (mu fixed) vsquish 
marginsplot, yline(0) title("Conditional Marginal Effect of Unemployed") ///
	subtitle("on Probability of Correct Response, with 95% CIs") ///
	xtitle("{&Delta}Unemployment since 2007") ///
	ytitle("Marginal Effect of Unemployed") ///
	xlabel(-5(5)20) scheme(s2mono) 

*/
	

esttab M1 M2 using Table1_Text.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) replace

****** APPENDIX: Robustness Checks *******
*1. Table 3: Baseline models*******
xtmelogit unempl_correct_cat || v6: , mle variance
eststo M1
di 0.56 / (0.56 + 3.29)
*14% of total variance can be explained by between-country differences
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 || v6: , mle variance
eststo M2
di (0.56 - 0.50) / 0.56
*Baseline political knowledge explains 11% of the variance at level 2
egen group = group(v6 EB)
egen pickone = tag(v6 EB)
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 i.EB || v6: , mle variance
eststo M3
predict u0, reffects
predict u0se, reses
sort u0
generate u0rank = sum(pickone)
serrbar u0 u0se u0rank if pickone==1, scale(1.96) yline(0) ///
xtitle("Country-year (ranked)") ytitle("Residuals") scheme(s2mono)
*Outliers, out of the [-1, 1] range are Slovenia and Poland.
list u0 u0se v6 if pickone==1
scatter u0 une_chng_crisis 
scatter u0 unempl 

esttab M1 M2 M3 using Table3_Appendix.rtf, cells(b(star fmt(3)) se(fmt(4))) ///
	stats(N, fmt(0)) legend label collabels(none) varlabels(_cons Constant) replace 

*2. Table 4: Models excluding outliers Slovenia and Poland (See Fig. 1 in Appendix)
preserve
drop if v6==28 | v6==26
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis i.EB ///
	|| v6: , mle variance
estadd scalar v1  = exp(2*[lns1_1_1]_b[_cons])
eststo M1_outl

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.EB ///
	|| v6: unemployed , mle variance cov(unstructured)
eststo M2_outl

cd "C:\Users\1437005\Dropbox\Publication\Anduiza_Marinova\Eurobarometer\Results"
esttab M1_outl M2_outl using Table4_Appendix.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) scalars(v1 "var(Unemployed)" v2 "var(Intercept)" ///
	cov "Covariance(Unemployed, Intercept)") replace
restore
	

*3. Table 5: Model with absolute levels of unemployment
*Note: Models do not converge unless I remove outliers!!
preserve
drop if v6==28 | v6==26
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 unempl female age age_2 ///
	educ_yrs i.occupation1 i.hh_children i.EB || v6: , mle variance
eststo M2_unempl
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children i.occupation1 unemployed##c.unempl ///
	i.EB || v6: unemployed , mle variance cov(unstructured)
eststo M1_unempl
esttab M2_unempl M1_unempl using Table5_Appendix.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) replace
restore

*4. Table 6: Model with 2% and 3% range of correct response
*With correct as within 2% of actual rate*
xtmelogit unempl_correct_wi2 mean_une_knowl_lag2007 une_chng_crisis female ///
	 age age_2 educ_yrs i.occupation1 i.hh_children i.EB || v6: , mle variance
eststo M1_wi2
xtmelogit unempl_correct_wi2 mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.EB  ///
	|| v6: unemployed , mle variance cov(unstructured)
eststo M2_wi2

*With correct as within 3% of actual rate*
xtmelogit unempl_correct_wi3 mean_une_knowl_lag2007 une_chng_crisis female ///
	 age age_2 educ_yrs i.occupation1 i.hh_children i.EB || v6: , mle variance
eststo M1_wi3
xtmelogit unempl_correct_wi3 mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.EB  ///
	|| v6: unemployed , mle variance cov(unstructured)
eststo M2_wi3

esttab M1_wi2 M2_wi2 M1_wi3 M2_wi3 using Table6_Appendix.rtf, ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace
	
	
*5. Table 7: Models with alternative measures of personal economic hardship
	*wealth is estimated based on the ownership of durable goods
	*diff_paying_bills is the difficulty paying bills in past year (1=never, 3=often)
recode diff_paying_bills (7=.)
	
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis ///
	wealth i.EB || v6: , mle variance
eststo M1

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed c.wealth##c.une_chng_crisis ///
	 i.EB|| v6: wealth , mle variance cov(unstructured)
eststo M3

/*
label variable wealth "Wealth, estimated"
label define wealthl -1 "1 SD below mean" 1 "1 SD above mean"
label define wealthl5 -1.5 "1.5 SD below mean" 1.5 "1.5 SD above mean"
label values wealth wealthl5
margins, at(mean_une_knowl_lag2007=.30 female=1 age=49.79 age_2=2771.23 ///
	educ_yrs=19.83 not_laborforce=0 student=0 hh_children=1 EB=2015 ///
	unemployed=0 une_chng_crisis=(-5(1)20) wealth=(-1.5 1.5)) predict (mu fixed)
marginsplot, recast(connected) recastci(rline) ///
	title("Predicted probabilities of economic knowledge") ///
	subtitle("by Wealth") xtitle("Change in Unemployment since 2007") ///
	ytitle("Probability of Correct Response") ylabel(0(.25)1) xlabel(-5(5)20) ///
	legend(lab(1 "1.5 SD below mean") lab(2 "1.5 SD above mean")) scheme(s2mono) 
*/

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis ///
	i.diff_paying_bills i.EB || v6: , mle variance
eststo M2

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed i.diff_paying_bills##c.une_chng_crisis ///
	i.EB || v6: diff_paying_bills , mle variance cov(unstructured)
eststo M4

/*
label variable diff_paying_bills "Difficulty paying bills last year"
label define diff_paying_billsl 1 "Almost never/Never" 2 "From time to time" 3 "Most of the time"
label values diff_paying_bills diff_paying_billsl
margins, at(mean_une_knowl_lag2007=.30 female=1 age=49.79 age_2=2771.23 ///
	educ_yrs=19.83 not_laborforce=0 student=0 hh_children=1 EB=2015 ///
	unemployed=0 une_chng_crisis=(-5(1)20) diff_paying_bills=(1 3))  predict (mu fixed)
marginsplot, recast(connected) recastci(rline) ///
	title("Predicted probabilities of economic knowledge") ///
	subtitle("by Difficulty paying bills") xtitle("Change in Unemployment since 2007") ytitle("Probability of Correct Response") ///
	ylabel(0(.25)1) xlabel(-5(5)20) scheme(s2mono)
*/

esttab M1 M2 M3 M4 using Table7_Appendix.rtf, ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace

* Table 8: Models with imputed values
mi est: xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.EB  ///
	|| v6: unemployed , mle variance
eststo M2_imp
mi est: xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis i.EB ///
	|| v6: , mle variance
eststo M1_imp

esttab M1_imp M2_imp using Appendix_Tbl_8.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) replace


*Table 9. Analysis of missing values
gen mi_unempl_correct_cat = unempl_correct_cat
recode mi_unempl_correct_cat (0/1=0) (.=1)
xtmelogit mi_unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis trust_stats ///
	i.EB || v6: , mle variance
eststo M1_mi
	
xtmelogit mi_unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis trust_stats ///
	i.EB || v6: unemployed , mle variance cov(unstructured)
eststo M2_mi

esttab M1_mi M2_mi using Appendix_Tbl_9.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) replace

*Interaction unempl and age
xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female educ_yrs ///
	i.hh_children not_laborforce student c.age##unemployed une_chng_crisis i.EB ///
	|| v6: unemployed, mle variance
margins , at(unemployed=(0 1) age=(20(1)80) ) predict (mu fixed)
	marginsplot, ///
	ytitle("Probability of correct response") plot1opts(lpattern("l")) ///
	plot2opts(lpattern("-")) recast(connected) recastci(rline) scheme(s2mono)

	
*** Controling for prior occupation

gen prior_occ_unempl = prior_occ
replace prior_occ_unempl = 0 if unemployed==0
recode prior_occ_unempl (6=0) (7/8=5)

label define prior_occ_unempl_l 0 "Working" 1 "Self-employed" 2 "Managers" 3 "Other white collar" 4 "Manual workers" 5 "Other: House, Retired, Student"
label values prior_occ_unempl prior_occ_unempl_l
 

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed une_chng_crisis i.prior_occ_unemp i.EB ///
	|| v6: , mle variance
eststo M198

xtmelogit unempl_correct_cat mean_une_knowl_lag2007 female age age_2 educ_yrs ///
	i.hh_children not_laborforce student unemployed##c.une_chng_crisis i.prior_occ_unemp  i.EB ///
	|| v6: unemployed , mle variance cov(unstructured)
eststo M298

esttab M198 M298 using Appendix_Tbl_10.rtf, cells(b(star fmt(3)) ///
	se(fmt(4))) stats(N, fmt(0)) legend label collabels(none) ///
	varlabels(_cons Constant) replace
